<template>
<div style="padding: 10px;">
    <el-row >
      <el-col>
        <div style="display: flex;">
          <div>
            <el-form :model="list_params" ref="searchForm"  size="small" :inline="true" @submit.native.prevent="() => handleQuery()">
              <el-form-item label="" label-width="80px" prop="name">
                <el-input v-model="list_params.name" placeholder="港口名称搜索" ></el-input>
              </el-form-item>
              <el-form-item label="" prop="category">
                <el-select filterable  filterable v-model="list_params.category" placeholder="请选择分类" clearable :style="{width: '100%'}">
                  <el-option v-for="dict in dict.type.port_type" :key="dict.value" :label="dict.label"
                             :value="dict.value"  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary"  size="mini" native-type="submit">搜索</el-button>
                <el-button  size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div style="flex: 1;"></div>
          <div>
<!--            <el-button>删除</el-button>-->
            <el-button @click="handleEnable(1)" >禁用</el-button>
            <el-button @click="handleEnable(0)" >启用</el-button>
            <!-- <el-button @click="handleExport">导出</el-button>
            <el-button>导入</el-button> -->
            <el-button type="primary" @click="addCustom" >添加</el-button>
          </div>
        </div>
      </el-col>
      <el-col>
        <div style="margin-top: 10px;">
          <el-table :data="table_data" @selection-change="handleSelectionChange"  :height="table_height">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="港口名称" prop="name"></el-table-column>
            <el-table-column label="类别" :formatter="formatterData" prop="category">

              <template slot-scope="scope">
                <dict-tag :options="dict.type.port_type" :value="scope.row.category"/>
              </template>
            </el-table-column>
            <el-table-column label="状态" >
              <template slot-scope="scope" >
              <div style="color: #111928;" v-if="scope.row.enable==0" >启用</div>
              <div style="color: #EB4646;" v-else >禁用</div>
              </template>
            </el-table-column>
            <el-table-column label="操作">

              <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                @click="editInfo(scope.row)"

              >编辑</el-button>
              <el-popconfirm
                title="确定要删除这条记录吗？"
                @confirm="c_delete(scope.row)"
              >
                <el-button
                  slot="reference"
                  size="mini"
                  type="text"
                >删除</el-button>
              </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="list_total>0"
            :total="list_total"
            :page.sync="list_params.pageNum"
            :limit.sync="list_params.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog  :visible.sync="add_custom_dialog_show" title="添加客户" width="680px" style="border-radius: 10px;" @close="close">
      <el-form  ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="港口名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入港口名称"  show-word-limit clearable
                         :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类别" prop="category">
              <el-select filterable  filterable v-model="formData.category" placeholder="请选择分类" clearable :style="{width: '100%'}">
                <el-option v-for="dict in dict.type.port_type" :key="dict.value" :label="dict.label"
                           :value="dict.value"  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="启用" prop="enable">
              <el-switch v-model="formData.enable"  :active-value="0" :inactive-value="1"  ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="title" style="background: #F3F4F6;margin: -20px;padding: 20px;height: 58px;border-radius: 10px;">新增港口</div>
      <div slot="footer" >
        <el-button @click="close" >取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list,add,handleEnable,detail,update,del } from "@/api/base/port";
import { getToken } from "@/utils/auth";
import { mapGetters } from 'vuex'
import store from '@/store'
export default {
  name: 'custom',
  dicts:['port_type'],
  ...mapGetters([
    'user_id'
  ]),
  data() {
    return {
      table_height:window.innerHeight - 300,
      table_height:window.innerHeight - 300,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      add_custom_dialog_show:false,
      edit_custom_dialog_show:false,
      table_data:[],
      tmp_data:[],
      formData:{
        name:'',
        category:'',
        enable:1,
        createBy:''
      },
      formDataInit:{
        name:'',
        category:'',
        enable:1,
        createBy:''
      },
      formEditData:{
        phoneNumber:'',
        customerName:'',
        customerNum:'',
        customerClassify:'',
        enable:1,
        data_enable:true,
        depositBank:'',
        accountName:'',
        account:'',
        pictures:'',
        createBy:''
      },
      pictures:[],
      rules:{
        name:[{
          required: true,
          message: '请输入港口名称',
          trigger: 'blur'
        }],
        category:[{
          required: true,
          message: '请选择港口类别',
          trigger: 'change'
        }],
      },
      ids:[],
      list_params:{
        pageNum:1,
        pageSize:10,
        name:'',
        category:''
      },
      list_total:0
    }
  },
  created() {
    this.getList()

    this.getDicts('category').then(res => {
      console.log(res)
      this.tmp_data = res.data
    });

  },
  methods:{
    getList(){
      list(this.list_params).then(res => {
        this.table_data = res.rows;
        this.list_total = res.total;
      })
    },
    formatterData(row){
      return this.selectDictLabel(this.tmp_data,row.category+'');
    },
    addCustom(){
      this.add_custom_dialog_show = true;
      this.$refs['elForm'].resetFields();
      this.formData = {
        ...this.formDataInit
      }
    },
    close(){
      this.$refs['elForm'].resetFields();
      this.add_custom_dialog_show = false
      this.formData = this.formDataInit
    },
    handleConfirm(){

        this.$refs['elForm'].validate((valid)=>{
          if (valid){
            this.formData.createBy = store.getters.user_id;
            // this.formData.enable = this.formData.data_enable?0:1
            console.log(this.formData)

            this.add_custom_dialog_show = false
            if (this.formData.id){
              update(this.formData).then(res=>{
                this.getList()
              })
            }else{
              add(this.formData).then(res=>{
                this.getList()
              })
            }
            // add(this.formData).then(res=>{
            //   this.getList()
            // })
            this.formData = this.formDataInit
            this.$refs['elForm'].resetFields();
          }
        });
    },
    handleUpdate(){
        this.$refs['elEditForm'].validate((valid)=>{
          if (valid){
            let formData = this.formEditData;
            formData.createBy = store.getters.user_id;
            formData.enable = formData.enable?0:1

            this.edit_custom_dialog_show = false
            console.log(formData)
            update(formData).then(res=>{
              this.getList()
            })
            this.$refs['elEditForm'].resetFields();
          }
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleExport() {
      this.download('xnf/customer/export', {
        ...this.queryParams
      }, `customer${new Date().getTime()}.xlsx`)
    },
    handleEnable(enable){
      console.log(this.ids);
      if (this.ids.length == 0){
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning'
        });
        return
      }
      handleEnable(this.ids,enable).then(res => {
        this.getList()
      })
    },
    editInfo(row){
      let id = row.id;
      detail(id).then(res => {
        this.formData = {
          ...this.formData,
          ...res.data,
          category: res.data.category+''
        }
        this.add_custom_dialog_show = true
      })
    },
    field101BeforeUpload(){},
    c_delete(row){
      let id = row.id;
      del(id).then(res=>{
        this.getList()
      })
    },
    handleQuery(){
      this.list_params.pageNum = 1;
      this.getList();
    },
    resetQuery(){
      this.$refs['searchForm'].resetFields();
      this.handleQuery()
    },
  }
}
</script>

<style scoped>

</style>
